define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {  
                   data:'',
                   prinrting:0,
                   mainorder:{},
                    popup3:0,
                    popup:0,
                    popup2:0,
                    remark:'',
                    refund_money:0
                };
            },
            el: '#pageOrderDetail',
            created: function() {
                this.fetchData();
            },
            methods: {
                fetchData: function() {
                     this.loadOrder();
                    
                } ,
                // onShow:function(){
                //    this.loadOrder();
                // },
              
                loadOrder(reload = 0) {
                  var that = this;
                   if(reload==1){
                      app.setPageData("oms/order",{reload:1});
                   }
                  this.loading = 1;
                  // app.get("mall/order/find", {
                  //   order_sn: this.$route.params.order_sn
                  // }, function (res) {
                  //   that.loading = 0;
                  //   if (res.code != 200) {
                  //     app.alert(res.msg);
                  //     return;
                  //   }
                  //   that.data= res.data;
                     
                  // });

                   app.get("apiv2/order/find", {
                    order_sn: this.$route.params.order_sn
                  }, function (res) {
                    that.loading = 0;
                    if (res.code != 200) {
                      app.alert(res.msg);
                      return;
                    }
                    that.mainorder= res.data;
                    that.data=res.data.sub_order;
                     
                  });
                }, 
                 show_refund_money: function (e) {
                    var order_sn = $(e.target).attr("order_sn");
                    var refund_money = $(e.target).attr("refund_money");
                    var paystatus = $(e.target).attr("paystatus");
                    var order_type = $(e.target).attr("order_type");
                    this.setData({
                        order_sn: order_sn,
                        refund_money: refund_money,
                        order_type:order_type
                     });
                    if(refund_money>0 && paystatus ==1){
                      this.setData({
                        popup3: 1
                     });
                    }else{
                      this.cancel_order();
                    }
              
            },
             hide_refund_money:function(){
              this.popup3=0;
            },
              
                       // 取消定
              cancel_order:function(){
                if(this.order_type=="shopping"){
                  this.shopping_cancel();
                }else if(this.order_type=="book"){
                  this.book_cancel();
                }else if(this.order_type=="eat_in_hall"){
                  this.eat_in_hall_cancel();
                }
              },
             
// 取消订单
              shopping_cancel(){
                var that = this;
                app.get('/mall/order/cancel', {
                        order_sn: this.order_sn,
                        refund_desc: this.remark,
                        refund_money: this.refund_money
                    }, function(data) {
                        if (data.code == 200) {
                           app.alert('操作成功');
                          that.loadOrder(1);
                        } else {
                            app.alert(data.msg);
                        }
                    });
              },


              //确认接单
              shopping_receiving(order_sn){
              
                var that = this;
                app.get('/mall/order/receiving', {
                        order_sn: order_sn
                    }, function(data) {
                        if (data.code == 200) {
                           app.alert('操作成功');
                          that.loadOrder(1);
                        } else {
                            app.alert(data.msg);
                        }
                    });
              },

              //配送完成
              shopping_complete(order_sn) {
                var that = this;
                app.get('/mall/order/complete', {
                  order_sn: order_sn
                }, function (data) {
                  if (data.code == 200) {
                    
                    app.alert('操作成功');
                    that.loadOrder(1);
                  } else {
                    app.alert(data.msg);
                  }
                });
              },

                     //选择配送
              deliverMallOrder:function(e){
                  var that = this;
                  this.shop_id = $(e.target).attr("shop_id");
                  this.order_sn = $(e.target).attr("order_sn");
                  app.handle([
                      {
                        text:'自营配送',
                        methods:function(){
                          that.popup=1;
                        }
                      },
                      {
                        text:'蜂鸟配送',
                        methods:function(){
                          that.popup2=1;
                        }
                      },
                  ]);
              },

              //自己配送
               deliver_diy:function(e){
 
                var that = this;
                that.setData({
                  loading:1,
                  disabled:1
                });
                app.post('mall/order/delivery_diy', {
                  order_sn: this.order_sn,
                  remark: $("#formSubmit").find("[name='remark']").val(),
                  tracking_number: $("#formSubmit").find("[name='tracking_number']").val(),
                  method: 'POST',
                }, function (data) {
                  if (data.code == 200) {
                    app.alert('操作成功');
                    that.popup=0;
                    that.loadOrder(1);
                  } else {
                    app.alert(data.msg);
                    that.setData({
                      loading: 0,
                      disabled: 0
                    });
                  }
                });
              },

              //蜂鸟配送
              deliver: function (e) {
                // remark: $("#formSubmit").find("[name='remark']").val(),
                //   tracking_number: $("#formSubmit").find("[name='tracking_number']").val(),
                //   shop_id: this.shop_id,
                var that = this;
                that.setData({
                  loading: 1,
                  disabled: 1
                });
                var url = 'mall/order/delivery_dada';
                if (this.data.type == 'ele') {
                   url = 'mall/order/delivery_ele';
                }
                app.post(url, {
                  order_sn: this.data.order_sn,
                  remark: $("#formSubmit2").find("[name='remark']").val(),
                  order_weight: $("#formSubmit2").find("[name='order_weight']").val(),
                  method: 'POST',
                }, function (data) {
                  if (data.code == 200) {
                    app.alert('操作成功');
                    that.popup2=0;
                    that.loadOrder(1);
                  } else {
                    app.alert(data.msg);
                    that.setData({
                      loading: 0,
                      disabled: 0
                    });
                  }
                });
              },
              setData:function(arr){
                  for( var i in arr){
                     this[i] = arr[i];
                  }
             },

              //打印小票
              orderPrint(e){
                var order_sn = $(e.target).attr("order_sn");
                var shop_id = $(e.target).attr("shop_id");
                var that = this;
                var tmp ={};
                tmp[order_sn] =1;  
                this.setData({
                  orderPrintloading: tmp ,
                  orderPrintdisabled: tmp
                });
                app.get('mall-admin/mall-order/orderPrint', {
                  order_sn: order_sn,
                  shop_id: shop_id
                }, function (data) {
                  tmp[order_sn]=0;
                  that.setData({
                    orderPrintloading: tmp,
                    orderPrintdisabled: tmp
                  });
                  if (data.error === 0) {
                    app.alert('打印指令发送成功');
                  } else {
                    app.alert(data.msg);
                  }
                });
              },
                topay:function(e){
                  
                }
 
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
